package rad.framework.jsf;

import java.io.IOException;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.el.ValueBinding;

public class HtmlTableCell extends UIComponentBase {

	public static final String COMPONENT_FAMILY = "rad.framework.jsf.HtmlTableCell";

	public static final String COMPONENT_TYPE = "rad.framework.jsf.HtmlTableCell";

	private String align;

	private String valign;

	private String colspan;

	private String width;

	@Override
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getValign() {
		return valign;
	}

	public void setValign(String valign) {
		this.valign = valign;
	}

	public String getColspan() {
		return colspan;
	}

	public void setColspan(String colspan) {
		this.colspan = colspan;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	@Override
	public void restoreState(FacesContext context, Object state) {
		Object[] values = (Object[]) state;
		super.restoreState(context, values[0]);
		align = (String) values[1];
		valign = (String) values[2];
		colspan = (String) values[3];
		width = (String) values[4];
	}

	@Override
	public Object saveState(FacesContext context) {
		Object[] values = new Object[5];
		values[0] = super.saveState(context);
		values[1] = align;
		values[2] = valign;
		values[3] = colspan;
		values[4] = width;
		return values;
	}

	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		if (!isRendered())
			return;

		super.encodeBegin(context);
		ResponseWriter response = context.getResponseWriter();
		response.startElement("td", this);

		response.writeAttribute("id", getClientId(context), "id");

		ValueBinding classBinding = getValueBinding("align");
		String align = classBinding == null ? this.align
				: (String) classBinding.getValue(context);
		if (align != null) {
			response.writeAttribute("align", align, "align");
		}

		classBinding = getValueBinding("valign");
		String valign = classBinding == null ? this.valign
				: (String) classBinding.getValue(context);
		if (valign != null) {
			response.writeAttribute("valign", valign, "valign");
		}

		classBinding = getValueBinding("colspan");
		String colspan = classBinding == null ? this.colspan
				: (String) classBinding.getValue(context);
		if (colspan != null) {
			response.writeAttribute("colspan", colspan, "colspan");
		}

		classBinding = getValueBinding("width");
		String width = classBinding == null ? this.width
				: (String) classBinding.getValue(context);
		if (width != null) {
			response.writeAttribute("width", width, "width");
		}
	}

	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		if (!isRendered())
			return;

		ResponseWriter response = context.getResponseWriter();
		response.endElement("td");
		response.flush();
		super.encodeEnd(context);
	}

}
